package ly.count.java.sdk;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.JSONWriter;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;

/**
 * Created by Jack.Zhou on 2016/7/6.
 */
public class FilePreferences implements SharedPreferences {
    private static JSONObject properties = new JSONObject();
    private static File perFile;
    private static FilePreferences instance = new FilePreferences();

    public void FilePreferences() {
        JSONReader reader = null;
        try {
            perFile = new File(FilePreferences.class.getResource("preferences.json").getFile());
            if (!perFile.exists()) {
                perFile.createNewFile();
            }
            reader = new JSONReader(new FileReader(perFile));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        properties = reader.readObject(JSONObject.class);
        reader.close();
    }

    public JSONObject getAll() {
        return properties;
    }

    public String getString(String key, String value) {
        return properties.containsKey(key) ? properties.getString(key) : value;
    }

    public Collection<? extends Object> getStringSet(String key, Set<String> value) {
        return properties.containsKey(key) ? properties.getJSONArray(key) : value;
    }

    public int getInt(String key, int value) {
        return properties.containsKey(key) ? properties.getInteger(key) : value;
    }

    public long getLong(String key, long value) {
        return properties.containsKey(key) ? properties.getLongValue(key) : value;
    }

    public float getFloat(String key, float value) {
        return properties.containsKey(key) ? properties.getFloatValue(key) : value;
    }

    public boolean getBoolean(String key, boolean value) {
        return properties.containsKey(key) ? properties.getBoolean(key) : value;
    }

    public boolean contains(String key) {
        return properties.containsKey(key);
    }

    public SharedPreferences putString(String key, String value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences putStringSet(String key, Set<String> value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences putInt(String key, int value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences putLong(String key, long value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences putFloat(String key, float value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences putBoolean(String key, boolean value) {
        this.properties.put(key, value);
        return this;
    }

    public SharedPreferences remove(String key) {
        return this;
    }

    public SharedPreferences clear() {
        this.properties.clear();
        return this;
    }

    public boolean commit() {
        try {
            apply();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public void apply() throws IOException {
        JSONWriter append = new JSONWriter(new FileWriter("preferences.json"));
        append.writeObject(properties);
        append.close();
    }


}
